package cqrtplm.aop;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.hustcad.plm.basic.util.TyplmEntityBaseUtil;
import com.hustcad.plm.pdm.classification.model.dto.TyplmClassificationEntityDTO;
import com.hustcad.plm.pdm.classification.service.TyplmClassificationService;
import com.hustcad.plm.pdm.type.service.TyplmTypeService;
import com.ty.basic.entity.IdentifierEntity;
import com.ty.basic.entity.type.ClassificationDO;
import com.ty.basic.vo.TyAttributeDefMapVO;
import cqrtplm.mapper.RTEnumMapper;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Aspect
@Component
@Slf4j
public class CopyAop {

    @Resource
    private TyplmClassificationService typlmClassificationService;
    @Resource
    private TyplmTypeService typlmTypeService;
    @Resource
    private RTEnumMapper rtEnumMapper;

    // 复制时，同步二开枚举
    @Around("execution(* com.hustcad.plm.pdm.classification.service.TyplmClassificationService.copyToParse(..))")
    @SneakyThrows
    public Object afterCopyToParse(ProceedingJoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        TyplmClassificationEntityDTO proceed = (TyplmClassificationEntityDTO)joinPoint.proceed();
        IdentifierEntity currEntity = (IdentifierEntity)args[0];
        ClassificationDO currentEntityInfo = typlmClassificationService.getClassificationByOID(currEntity.getOid());
        List<TyAttributeDefMapVO> currentAttrDefMapList = this.typlmTypeService.queryInheritAttrDef(currentEntityInfo.getOid(), currentEntityInfo.getOtype());
        currentAttrDefMapList.forEach((attr) -> {
            attr.setIbaDefType(TyplmEntityBaseUtil.getAttributeTypeDisplay(attr.getIbaDefType()));
        });

        Map<String, TyAttributeDefMapVO> currentClassIbaMap = currentAttrDefMapList.stream().collect(Collectors.toMap(
                TyAttributeDefMapVO::getName,
                item -> item,
                (existing, replacement) -> existing
        ));


        ClassificationDO newEntityInfo = typlmClassificationService.getClassificationByOID(new BigInteger(proceed.getId()));
        List<TyAttributeDefMapVO> newAttrDefMapList = this.typlmTypeService.queryInheritAttrDef(newEntityInfo.getOid(), newEntityInfo.getOtype());
        newAttrDefMapList.forEach((attr) -> {
            attr.setIbaDefType(TyplmEntityBaseUtil.getAttributeTypeDisplay(attr.getIbaDefType()));
        });

        Map<String, TyAttributeDefMapVO> newClassIbaMap = newAttrDefMapList.stream().collect(Collectors.toMap(
                TyAttributeDefMapVO::getName,
                item -> item,
                (existing, replacement) -> existing
        ));

        for (Map.Entry<String, TyAttributeDefMapVO> entry : currentClassIbaMap.entrySet()) {
            String currentKey = entry.getKey();
            if(newClassIbaMap.containsKey(currentKey)){
                TyAttributeDefMapVO currentAttr = entry.getValue();
                TyAttributeDefMapVO newAttr = newClassIbaMap.get(currentKey);
                JSONObject select = rtEnumMapper.select(String.valueOf(currentAttr.getOid()));
                if(ObjectUtil.isNotEmpty(select)){
                    rtEnumMapper.insertEnumDescription(
                            select.getString("ENUMOID"),
                            select.getString("DESCRIPTION"),
                            String.valueOf(newAttr.getOid()),
                            select.getString("PARENTENUMOID")
                    );
                }
            }
        }
        return proceed;
    }
}
